$(function () {
    var employee_dig = $("#employee_dig");
    var employee_dg = $('#employee_dg');
    var employee_form = $('#employee_form');
    /*给所有的a标签绑定点击事件*/

    var ObjectCmd ={
        /*保存*/
        saveUser:function () {
            $("#checkUsername").prop("readonly",false)
            /*给用户名绑定事件*/
            $("#checkUsername").blur(function () {
                    var usernmae =this.value;
                    $.get("/employee/checkUsername.do",{'username':usernmae},function (data) {
                        if (data.success) {
                            $("#usernamecss").html('')
                        }else {
                            $("#usernamecss").html(data.msg)

                        }

                    })

                }
            )
            employee_form.form("clear")
            $("#password").show();
            employee_dig.dialog("setTitle", "add");
            employee_dig.dialog("open");
            
        },
        /*编辑*/
        editUser: function editUser() {
            var tr = employee_dg.datagrid("getSelected");

            if (!tr) {
                //没有选中
                $.messager.alert('温馨提示', "至少选中一条数据");
                return;
            }
            tr["dept.id"]=tr.dept.id;
            //处理员工角色回显的问题(回显多个角色)(根据员工的id查询出有多少角色,然后封装到data中,在setValues方法上填充到下拉框中)
            $.get('/employee/roleIds.do',{employeeId:tr.id}, function (data) {
                console.log(data);
                $("#role_combobox").combobox('setValues', data);})
            employee_form.form("clear")
            //隐藏
            $("#password").hide()
            //用户名只读
            $("#checkUsername").prop("readonly",true)
            employee_dig.dialog("setTitle", "editUser");
            employee_dig.dialog("open");
            employee_form.form("load", tr)
        },
        /*离职*/
        changeUser: function () {
            var tr = employee_dg.datagrid("getSelected");
            if (!tr) {
                //没有选中
                $.messager.alert('温馨提示', "至少选中一条数据");
                return;
            }
            $.get("/employee/changeState.do", {id: tr.id}, function (data) {
                if (data.success) {
                    $.messager.alert('温馨提示', '操作成功', 'info', function () {


                        employee_dg.datagrid("reload");
                    });
                }
                else {
                    $.messager.alert('温馨提示', data.msg);


                }
            })
        },
        /*刷新*/
        reloadwin: function () {
            employee_dg.datagrid("reload");
        },
        /*高级查询*/
        queryForKey: function () {
            var keyword=  $("input[name='keyword']").val();
            employee_dg.datagrid("load",{"keyword":keyword});
        },
        /*关闭对话框*/
        closeWin:function () {
            employee_dig.dialog("close")

        },
        /*导出*/
        exportData:function () {
            //发送异步请求
            window.location.href = "/employee/exportData.do";
        },
        /*提交表单*/
        submitForm:function () {
            /*提交角色*/
            employee_form.form({
                url: '/employee/saveOrUpdate.do',
                onSubmit:function (param) {
                   var roleIds = $("#role_combobox").combobox('getValues')
                    console.log(roleIds);

                    $.each(roleIds,function (i,ele) {
                        param["roles["+i+"].id"] =ele;
                    })
                },
                success: function (data) {
                    var data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('温馨提示', '保存成功', 'info', function () {

                            ObjectCmd.closeWin();
                            employee_dg.datagrid("reload");
                        });
                    }
                    else {
                        $.messager.alert('温馨提示', data.msg);
                        ObjectCmd.closeWin();

                    }
                }
            });


            employee_form.submit();
        }
    }
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd")
        ObjectCmd[cmd]();
    })





    //编辑对话框
    employee_dig.dialog({
        width: 400,
        height: 400,
        closed: true,
        cache: false,
        modal: true,
        buttons: '#employee_dig_btn'
    })
    //页面table
    employee_dg.datagrid({
        url: '/employee/query.do',
        fitColumns: true,
        fit: true,
        pagination: true,
        singleSelect: true,
        toolbar: "#employee_tb",

        rownumbers: true,

        onClickRow: function (index, row) {

            if (row.state) {

                $("#btn_change").linkbutton({iconCls: 'icon-no', text: '离职'})
            }
            else {
                $("#btn_change").linkbutton({iconCls: 'icon-ok', text: '复职'})

            }
        },
        columns: [[
            {field: 'id', title: '编号', width: 100},
            {field: 'username', title: '员工名称', width: 100},
            {field: 'realname', title: '真实姓名', width: 100},
            {field: 'tel', title: '联系方式', width: 100},
            {field: 'email', title: '邮箱', width: 100},
            {
                field: 'dept', title: '所属部门', width: 100, formatter: function (value, row, index) {
                    if (value) {
                        return value.name;
                    } else {
                        return null;
                    }
                }
            },
            {field: 'hiretime', title: '入职时间', width: 100},
            {
                field: 'state', title: '状态', width: 100, formatter: function (value, row, index) {
                    if (value) {
                        return "在职";
                    } else {
                        return "<span style='color: red'>离职</span>";
                    }
                }
            },
            {field: 'admin', title: '超级管理员', width: 100}

        ]]
    });
})




